package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.GrouponProductListBean;
import com.ymatou.iapi.productmanager.parameter.GrouponProductListDto;
import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GrouponProductListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.GrouponCatalogs;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
*获取拼团商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GrouponProductList{
	private static GrouponProductListBean grouponproductlistBean;
	private static GrouponProductListCall grouponproductlistCall;
	private static YmtProductsIWapper YmtProductsIWapper;
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	/**
	 * 非优选
	 */
	private static Integer sellerId2 = Integer.valueOf(EnvSetup.getData("autotest_seller1"));
	/**
	 * 优选
	 */
	private static Integer sellerId = 3383;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取拼团商品列表");
	}

	@Before
	public void caseUp() {
		grouponproductlistBean=new GrouponProductListBean();
		grouponproductlistCall=new GrouponProductListCall();
		YmtProductsIWapper = new YmtProductsIWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(grouponproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void setStock(AddSellerProductRequest addpro, Integer catalogNum) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
	}

	private void setPrice(AddSellerProductRequest addpro, Double price) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setNewPrice(price);
			catalog.setMarketPrice(price);
			catalog.setVipPrice(price);
			catalog.setPrice(price);
		});
	}

	public void verifyDB(List<GrouponProductListDto> grouponProductListDtos){
		Logger.debug("grouponProductListDtos:"+ grouponProductListDtos);
		grouponproductlistCall.getProductList().forEach(p->{
			YmtProductsI ymtProductsI = null;
			try {
				ymtProductsI = ymtProductsIWapper.selectProductByProductId(p.getProductId()).get(0);
				List<YmtCatalogsI> catalogsIS = ymtCatalogsIWapper.selectCatalogsByProductId(p.getProductId());
				List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(p.getProductId(), null, 0);
				Integer totalStock = 0;
				for (YmtCatalogsI x:catalogsIS
				) {
					totalStock = totalStock+x.getiNum();
				}
				Logger.verifyEquals(p.getProductName(),ymtProductsI.getsProduct(),"验证productName");
				Logger.verifyEquals(p.getThirdCategoryId(),ymtProductsI.getiThirdCategoryId(),"验证ThirdCategoryId");
				Logger.verifyEquals(p.getBrandId(),ymtProductsI.getiBrandId(),"验证BrandId");
				Logger.verifyEquals(p.getMainPicUrl(),ymtProductsI.getsPicUrl(),"验证MainPicUrl");
				Logger.verifyEquals(p.getCatalogStatus(),ymtProductsI.getiCatalogStatus(),"验证CatalogStatus");
				Logger.verifyEquals(p.getProductStock(),totalStock,"验证ProductStock");
				Logger.verifyEquals(p.isFreeTariff(),ymtProductsI.getiTariffType()==0,"验证FreeTariff");
				List<YmtCatalogsI> ymtCatalogsIS = new YmtCatalogsIWapper().selectCatalogsByProductId(p.getProductId());
				double MinCatalogPrice = Collections.min(ymtCatalogsIS.stream().filter(c->c.getiAction()==0).map(c -> c.getfQuotePrice()).collect(Collectors.toList())).doubleValue();
				double MaxCatalogPrice = Collections.max(ymtCatalogsIS.stream().filter(c->c.getiAction()==0).map(c -> c.getfQuotePrice()).collect(Collectors.toList())).doubleValue();
				Logger.verifyEquals(p.getMaxCatalogPrice(),MaxCatalogPrice,"验证MaxCatalogPrice");
				Logger.verifyEquals(p.getMinCatalogPrice(),MinCatalogPrice,"验证MaxCatalogPrice");
				Logger.verifyEquals(p.getGrouponPerson(),gp.get(0).getGrouponMemberCount(),"验证GrouponPerson");
				Logger.verifyEquals(p.getGroupProductId(),gp.get(0).getGrouponProductId(),"验证GroupProductId");
				Logger.verifyEquals(p.getMinGrouponStock(),20,"验证MinGrouponStock");
				Logger.verifyEquals(p.getMaxGrouponPriceLimit(),300.0,"验证MaxGrouponPriceLimit");
				Long version = gp.get(0).getVersion();
//				List<GrouponCatalogs> catalogs = YmtProductsIWapper.selectGrouponCatalogs(gc.getCatalogId(), version.intValue());
				List<Double> groupPriceList = new ArrayList<>();
				ymtCatalogsIS.forEach(c->{
					List<GrouponCatalogs> catalogs = YmtProductsIWapper.selectGrouponCatalogs(c.getsCatalogId(), version.intValue());
					groupPriceList.add(catalogs.get(0).getGrouponPrice().doubleValue());
				});
				Logger.verifyEquals(p.getMaxGroupPrice(),Collections.max(groupPriceList),"验证MaxGroupPrice");
				Logger.verifyEquals(p.getMinGroupPrice(),Collections.min(groupPriceList),"验证MinGroupPrice");

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
	}

	//商品数据从ES中查出来
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponProductList_01() {
		Logger.start(true,"买手无拼团商品，获取拼团商品列表");
		try {
			grouponproductlistBean.setSellerId(sellerId2);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(10);
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();			
			Logger.verifyEquals("200",grouponproductlistCall.getString("Code"),"验证Code");
			Logger.verifyEquals(0,grouponproductlistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsInt(),"验证买手是否是拼团商品");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponProductList_02() {
		Logger.start(true,"买手有单个拼团商品，根据商品名称模糊查询，获取拼团商品列表");
		try {
			//创建一个拼团商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().setProductName("拼团"+ TestDataManager.getRandomNum(6));
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);

			Thread.sleep(5000);
			grouponproductlistBean.setSellerId(sellerId);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(10);
			grouponproductlistBean.setProductName("拼团");
//			grouponproductlistBean.setThirdCategoryId(addpro.getProduct().getThirdCategoryId());
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();
			Logger.verifyEquals("200",grouponproductlistCall.getString("Code"),"验证Code");
			List<GrouponProductListDto> grouponProductListDtos = grouponproductlistCall.getProductList();
			verifyDB(grouponProductListDtos);
			grouponProductListDtos.forEach(p->{
				int status = YmtProductsIWapper.selectGrouponProducts(p.getProductId(), null, 0).get(0).getStatus();
				Logger.verifyEquals(0,status,"验证Status状态");
				Logger.verifyEquals(true,p.getProductName().contains("拼团"),"验证商品名称");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponProductList_03() {
		Logger.start(true,"根据商品三级类目，获取拼团商品列表");
		try {
			//创建一个拼团商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			addpro.getProduct().setThirdCategoryId(1003);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);

			Thread.sleep(5000);
			grouponproductlistBean.setSellerId(sellerId);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(10);
			grouponproductlistBean.setThirdCategoryId(addpro.getProduct().getThirdCategoryId());
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();
			Logger.verifyEquals("200",grouponproductlistCall.getString("Code"),"验证Code");
			List<GrouponProductListDto> grouponProductListDtos = grouponproductlistCall.getProductList();
			verifyDB(grouponProductListDtos);
			Logger.verifyEquals(true,grouponProductListDtos.size()==10,"验证返回结果是10个商品");
			grouponProductListDtos.forEach(p->{
				int status = YmtProductsIWapper.selectGrouponProducts(p.getProductId(), null, 0).get(0).getStatus();
				Logger.verifyEquals(0,status,"验证Status状态");
				Logger.verifyEquals(1003,p.getThirdCategoryId(),"验证ThirdCatalog");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//不做校验
	public void Tc_GrouponProductList_07() {
		Logger.start(true,"买手id为空，获取拼团商品列表");
		try {
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setPageSize(10);
			grouponproductlistBean.setProductName("拼团");
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponProductList_08() {
		Logger.start(true,"指定拼团人数，验证拼团人数");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().setProductName("拼团"+ TestDataManager.getRandomNum(6));
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			addpro.getProduct().setThirdCategoryId(1003);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			int num = 10;
			saveGrouponBean.setGrouponMemberCount(num);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);

			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setSellerId(sellerId);
			grouponproductlistBean.setPageSize(10);
			grouponproductlistBean.setProductName("拼团");
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();
			Logger.verifyEquals("200",grouponproductlistCall.getString("Code"),"验证Code");
			List<GrouponProductListDto> grouponProductListDtos = grouponproductlistCall.getProductList();
			verifyDB(grouponProductListDtos);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponProductList_09() {
		Logger.start(true,"指定拼团人数，验证拼团人数");
		try {
			int sellerId=3383;
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addProdReq.getProduct().setCatalogStatus(10);
			addProdReq.getProduct().setDeliveryCompanyType(1);
			addProdReq.getProduct().setDeliveryAging(5);
			setStock(addProdReq, 30);
			setPrice(addProdReq, 100d);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			int num = 10;
			saveGrouponBean.setGrouponMemberCount(num);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			
			
			Thread.sleep(3000);
			grouponproductlistBean.setPageIndex(1);
			grouponproductlistBean.setSellerId(sellerId);
			grouponproductlistBean.setPageSize(10);
			grouponproductlistBean.setProductName(addProdReq.getProduct().getProductName());
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();
			grouponproductlistCall.setData(grouponproductlistBean);
			grouponproductlistCall.callService();
			
			Logger.verifyEquals("200",grouponproductlistCall.getString("Code"),"验证Code");
			List<GrouponProductListDto> grouponProductListDtos = grouponproductlistCall.getProductList();
			verifyDB(grouponProductListDtos);
			Logger.verifyEquals(1,grouponProductListDtos.get(0).getDeliveryCompanyType(),"验证DeliveryCompanyType");
			Logger.verifyEquals(5,grouponProductListDtos.get(0).getDeliveryAging(),"验证DeliveryAging");
			Logger.verifyEquals(10,grouponProductListDtos.get(0).getCatalogStatus(),"验证CataLogStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}



}